import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout, QLabel
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QFileDialog

class FileModeDemo(QWidget):
    def __init__(self):
        super().__init__()
        self.init_ui()
        
    def init_ui(self):
        layout = QVBoxLayout()
        
        self.info_label = QLabel('文件模式演示')
        
        modes = [
            ("任何文件", QFileDialog.AnyFile),
            ("现有文件", QFileDialog.ExistingFile),
            ("目录", QFileDialog.Directory),
            ("多个文件", QFileDialog.ExistingFiles)
        ]
        
        for name, mode in modes:
            btn = QPushButton(f'{name}模式')
            btn.clicked.connect(lambda checked, m=mode: self.demo_file_mode(m))
            layout.addWidget(btn)
        
        layout.addWidget(self.info_label)
        self.setLayout(layout)
        
    def demo_file_mode(self, file_mode):
        dialog = QFileDialog(self)
        dialog.setFileMode(file_mode)
        dialog.setWindowTitle(f'文件模式: {file_mode}')
        
        if dialog.exec_() == QFileDialog.Accepted:
            files = dialog.selectedFiles()
            self.info_label.setText(f"选择了 {len(files)} 个项目:\n" + "\n".join(files))

            
if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = FileModeDemo()
    window.show()
    sys.exit(app.exec_())